﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Data.Objects;
using Model;

namespace DAL_EF
{
    public class SaveRepository : ISaveRepository
    {


        /// <summary>
        /// save course
        /// </summary>
        public int insertCourse(Model.Course course)
        {

            using (var edm = new ProjectbEntities())
            {
                Course _course = EntityConverter.ToEFCourse(course);
                edm.Courses.AddObject(_course);
                edm.SaveChanges();
                return _course.CourseId;
            }
        }
        /// <summary>
        /// update 
        /// </summary>
        /// <param name="course"></param>
        public void updateCourse(Model.Course course)
        {
            using (var edm = new ProjectbEntities())
            {
                Course _course = edm.Courses.SingleOrDefault(c => c.CourseId == course.CourseId);
                _course.CourseId = course.CourseId;
                _course.CourseName = course.CourseName;
                _course.LevelId = course.LevelId;
                _course.TeacherId = course.TeacherId;
                edm.SaveChanges();

            }
        }

        public void insertCourseDetails(Model.CourseDetails cd)
        {
            using (var edm = new ProjectbEntities())
            {
                CourseDetail coursedetails = EntityConverter.ToEFCourseDetails(cd);
                edm.CourseDetails.AddObject(coursedetails);
                edm.SaveChanges();
            }

        }
        public void deleteCourseDetails(int cID)
        {
            using (var edm = new ProjectbEntities())
            {
                var coursedetails = (from c in edm.CourseDetails
                                     where c.CourseId == cID
                                     select c).ToList<CourseDetail>();
                //CourseDetail coursedetail = edm.CourseDetails.SingleOrDefault(c => c.CourseId == cID);

                foreach (CourseDetail obj in coursedetails)
                {
                    edm.DeleteObject(obj);

                }
                edm.SaveChanges();

            }
        }
        public List<Model.Course> getCourseName(int courseId)
        {
            using (var edm = new ProjectbEntities())
            {
                var courses = (from c in edm.Courses
                               where c.CourseId == courseId
                               select c).ToList<Course>();

                return EntityConverter.ToModelCourse(courses);
            }
        }

    }
}



